<?php
namespace Admin\Model;
use Common\Model;
if(!defined('SITE')) exit('Access Denied');
class ArticleModel extends Model {
	protected $tableNameType='sfk_arctype'; 
	public function __construct(){
		$this->tableName='article';
		$this->validate=array(
			'id'=>array(
				'between'=>'0,4294967295',
				'unique'=>true
			),
			'atid'=>array(
				'between'=>'1,4294967295',
				'method'=>array('checkAtid')
			),
			'title'=>array(
				'length'=>'1,32'
			),
			'keywords'=>array(
				'length'=>'1,128'
			),
			'description'=>array(
				'length'=>'1,512'
			),
			'editor'=>array(
				'length'=>'1,32'
			),
			'eid'=>array(
				'between'=>'0,4294967295'
			),
			'pubdate'=>array(
				'between'=>'0,4294967295'
			),
			'weights'=>array(
				'between'=>'0,4294967295'
			),
			'litpic'=>array(
				'length'=>'1,512'
			),
			'isdelete'=>array(
				'in'=>'0,1'
			)
		);
		$this->vTime=array(
			'editor'=>array(
				'null'=>array(self::INSERT)
			),
			'eid'=>array(
				'null'=>array(self::INSERT)
			),
			'pubdate'=>array(
				'null'=>array(self::INSERT)
			)
		);
		$this->vMessage=array(
			'atid'=>array(
				'method'=>'栏目不存在'
			)
		);
		$this->vFieldsAlias=array(
			'atid'=>'所属栏目',
			'title'=>'新闻标题',
			'keywords'=>'关键字',
			'description'=>'描述',
			'content'=>'内容',
			'editor'=>'责任编辑',
			'eid'=>'管理员id',
			'pubdate'=>'发布日期',
			'weights'=>'权重',
			'litpic'=>'缩略图',
			'isdelete'=>'是否删除'
		);
		parent::__construct();
	}
	public function checkAtid($atid){
		$param=array(
				'sql'=>"select `id` from `{$this->tableNameType}` where id=?",
				'bind'=>array('i',array($atid))
		);
		return count($this->db->execute($param));
	}
	public function add($data,$autoValidation=true){
		$data['editor']='孙胜利';
		$data['eid']=1;
		$data['pubdate']=time();
		if(is_array(post('flag'))){
			$data['flag']=implode(',',post('flag'));
		}
		return parent::add($data,$autoValidation);
// 		if(post('pid')==0){
// 			$data['tid']=$this->getNextAI();
// 		}else{
// 			$data['tid']=$this->getTid(post('pid'));
// 		}
// 		return parent::add($data,$autoValidation);
	}
	public function selectByPage($limit,$isDelete=0){
		$param=array(
			'sql'=>"SELECT a.weights,a.id,a.title,b.name,a.editor,a.pubdate FROM {$this->getTableName()} a,{$this->tableNameType} b WHERE a.atid=b.id AND isdelete={$isDelete} {$limit}"
		);
		return $this->db->execute($param);
	}
	public function update($data,$pk,$autoValidation=true){
		if(isset($data['editor'])){
			unset($data['editor']);
		}
		if(isset($data['eid'])){
			unset($data['eid']);
		}
		if(isset($data['pubdate'])){
			unset($data['pubdate']);
		}
		if(is_array(post('flag'))){
			$data['flag']=implode(',',post('flag'));
		}else{
			$data['flag']=null;
		}
		return parent::update($data,$pk,$autoValidation);
	}
	public function updateIsDelete($pk,$isDelete=1){
		$param=array(
			'sql'=>"UPDATE {$this->getTableName()} SET isdelete={$isDelete} WHERE {$this->getPK()}=?",
			'bind'=>array($this->getFieldsType($this->getPK()),array($pk))
		);
		return $this->db->execute($param);
	}
	
}